//
//  TestMKUIElementViewSlider_CGSize.m
//  MKUITestApp
//
//  Created by 苏尚进 on 2025/7/11.
//  Copyright © 2025 moon. All rights reserved.
//

#import "TestMKUIElementViewSlider_CGSize.h"
#import "TestSliderComponent.h"
@interface TestMKUIElementViewSliderList_CGSize : UIView
@property(nonatomic,strong) NSArray<TestSliderComponent *> *sliderList;
@property(nonatomic,assign) CGSize value;
@property(nonatomic,assign) CGSize minimumValue;
@property(nonatomic,assign) CGSize maximumValue;
@property(nonatomic,copy,nullable) void(^whenChange)(TestMKUIElementViewSliderList_CGSize *el,CGSize value);
@property(nonatomic,strong) MKUIFlowLayoutConstraint *flowlayout;
@end

@implementation TestMKUIElementViewSliderList_CGSize
- (id)initWithFrame:(CGRect)frame{
    if(self=[super initWithFrame:frame]){
        @MK_WEAKIFY(self);
        self.sliderList = @[
            [[TestSliderComponent alloc] initWithTitle:@"width" value:0 minimumValue:0 maximumValue:0 whenChange:^(TestSliderComponent * _Nonnull el, CGFloat value) {@MK_NORMALIZE(self);[self _onSliderValueChange];}],
            [[TestSliderComponent alloc] initWithTitle:@"height" value:0 minimumValue:0 maximumValue:0 whenChange:^(TestSliderComponent * _Nonnull el, CGFloat value) {@MK_NORMALIZE(self);[self _onSliderValueChange];}],
        ];
        for(TestSliderComponent *c in self.sliderList){
            [self addSubview:c];
        }
        self.flowlayout = [[MKUIFlowLayoutConstraint alloc] initWithItems:self.sliderList constraintParam:(MKUIFlowLayoutConstraintParam_V_C_R) contentInsets:UIEdgeInsetsZero interitemSpacing:10];
    }
    return self;
}
- (void)layoutSubviews{
    [super layoutSubviews];
    self.flowlayout.bounds = self.bounds;
    [self.flowlayout layoutItemsWithResizeItems:YES];
}
- (CGSize)sizeThatFits:(CGSize)size{
    CGSize s = [self.flowlayout sizeThatFits:CGSizeMake(size.width, 9999) resizeItems:YES];
    return s;
}
- (void)setValue:(CGSize)value{
    _value = value;
    [self reloadData];
}
- (void)setMinimumValue:(CGSize)minimumValue{
    _minimumValue = minimumValue;
    [self reloadData];
}
- (void)setMaximumValue:(CGSize)maximumValue{
    _maximumValue = maximumValue;
    [self reloadData];
}
- (void)reloadData{
    self.sliderList[0].minimumValue = self.minimumValue.width;
    self.sliderList[1].minimumValue = self.minimumValue.height;
    
    self.sliderList[0].maximumValue = self.maximumValue.width;
    self.sliderList[1].maximumValue = self.maximumValue.height;
    
    self.sliderList[0].value = self.value.width;
    self.sliderList[1].value = self.value.height;
    [self setNeedsLayout];
}
- (void)_onSliderValueChange{
    self.value = CGSizeMake(self.sliderList[0].value, self.sliderList[1].value);
    if(self.whenChange){
        self.whenChange(self, self.value);
    }
}
@end

@interface TestMKUIElementViewSlider_CGSize()
@property(nonatomic,strong) TestMKUIElementViewSliderList_CGSize *sliderView;
@end

@implementation TestMKUIElementViewSlider_CGSize

- (id)initWithFrame:(CGRect)frame{
    if(self=[super initWithFrame:frame]){
        self.sliderView = [[TestMKUIElementViewSliderList_CGSize alloc] init];
        @MK_WEAKIFY(self);
        self.sliderView.whenChange = ^(TestMKUIElementViewSliderList_CGSize *el, CGSize value) {
            @MK_NORMALIZE(self);
            [self setNeedsLayout];
            if(self.whenChange){
                self.whenChange(self, self.value);
            }
        };
        self.accessView = self.sliderView;
    }
    return self;
}
- (CGSize)sizeThatFits:(CGSize)size{
    CGSize s = [self sizeThatFits:size contentSizeBlock:^CGSize(CGSize contentSize) {
        return CGSizeMake(contentSize.width, 44);
    }];
    return s;
}
- (void)setValue:(CGSize)value{
    self.sliderView.value = value;
}
- (CGSize)value{
    return self.sliderView.value;
}
- (CGSize)maximumValue{
    return self.sliderView.maximumValue;
}
- (void)setMaximumValue:(CGSize)maximumValue{
    self.sliderView.maximumValue = maximumValue;
}
- (CGSize)minimumValue{
    return self.sliderView.minimumValue;
}
- (void)setMinimumValue:(CGSize)minimumValue{
    self.sliderView.minimumValue = minimumValue;
}
@end

@implementation TestMKUIElementViewSlider_CGSize(Test)
+ (TestMKUIElementViewSlider_CGSize *)testSliderElementWithTitle:(NSString *)title value:(CGSize)value minimumValue:(CGSize)minimumValue maximumValue:(CGSize)maximumValue whenChange:(void(^)(TestMKUIElementViewSlider_CGSize *el,CGSize value))block{
    TestMKUIElementViewSlider_CGSize *el = [[TestMKUIElementViewSlider_CGSize alloc] init];
    el.titleText = title;
    el.minimumValue = minimumValue;
    el.maximumValue = maximumValue;
    el.value = value;
    el.whenChange = block;
    return el;
}
@end
